﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;

namespace Web.Pages.YongShui.YongHuGL.HuJiGL
{
    public partial class XiaoBiao : System.Web.UI.Page
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private string SHUIBIAOBH
        {
            get
            {
                return Convert.ToString(ViewState["_SHUIBIAOXXID"]);
            }
            set
            {
                ViewState["_SHUIBIAOXXID"] = value;
            }
        }

        /// <summary>
        /// 当前操作用户的用户号
        /// </summary>
        private string YONGHUH
        {
            get
            {
                return Convert.ToString(ViewState["_YONGHUH"]);
            }
            set
            {
                ViewState["_YONGHUH"] = value;
            }
        }

        /// <summary>
        /// 当前操作用户站点
        /// </summary>
        private string ZHANDIANBH
        {
            get
            {
                return Convert.ToString(ViewState["_ZHANDIANBH"]);
            }
            set
            {
                ViewState["_ZHANDIANBH"] = value;
            }
        }

        /// <summary>
        /// 记录当前查询的水表号,保存在dictionary中,方便实现查询内容记忆功能
        /// </summary>
        private Dictionary<string, string> shuiBiaoBHs
        {
            get
            {
                if (ViewState["shuiBiaoBHs"] == null)
                {
                    ViewState["shuiBiaoBHs"] = new Dictionary<string, string>();

                }
                return ViewState["shuiBiaoBHs"] as Dictionary<string, string>;
            }
        }

        /// <summary>
        /// 记录本次增表的水表号,保存在dictionary中,方便实现本次增表错误可进行即时删除
        /// </summary>
        private Dictionary<string, string> zbshuiBiaoBHs
        {
            get
            {
                if (ViewState["zbshuiBiaoBHs"] == null)
                {
                    ViewState["zbshuiBiaoBHs"] = new Dictionary<string, string>();

                }
                return ViewState["zbshuiBiaoBHs"] as Dictionary<string, string>;
            }
        }

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 记录列表二查询结果集
        /// </summary>
        private DataTable dtSource2
        {
            get
            {
                return ViewState["dtSource2"] as DataTable;
            }
            set
            {
                ViewState["dtSource2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 用户水表关系BLL
        /// </summary>
        YS_CB_YONGHUSBGXBLL _YS_CB_YONGHUSBGXBLL = new YS_CB_YONGHUSBGXBLL();

        /// <summary>
        /// 水表信息BLL
        /// </summary>
        YS_CB_SHUIBIAOXXBLL _YS_CB_SHUIBIAOXXBLL = new YS_CB_SHUIBIAOXXBLL();

        /// <summary>
        /// 自来水公司数据BLL
        /// </summary>
        YS_CB_ZILAISGSSJBBLL _YS_CB_ZILAISGSSJBBLL = new YS_CB_ZILAISGSSJBBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }
            gvList1.DataSource = dtSource;
            gvList1.DataBind();
            //SetDeleteVisible();
            //gvList2.DataSource = dtSource2;
            //gvList2.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData(true);
        }

        protected void lnbDel_Command1(object sender, CommandEventArgs e)
        {
            throw new NotImplementedException();
        }

        //protected void btnSearch2_Click(object sender, EventArgs e)
        //{
        //    LoadData2(true);
        //}

        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 控制删除是否显示
        /// </summary>
        private void SetDeleteVisible()
        {
            //根据是否是本页面新增表显示删除
            for (int i = 0; i < gvList1.VisibleRowCount; i++)
            {
                DataRowView drv = (DataRowView)gvList1.GetRow(i);
                LinkButton cb = (LinkButton)gvList1.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList1.Columns[3], "lnbDel");
                if (zbshuiBiaoBHs.ContainsKey(Convert.ToString(drv.Row["S_SHUIBIAOBH"])))
                {
                    cb.Visible = true;
                }
                else
                {
                    cb.Visible = false;
                }
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            if (YONGHUH != "")
            {
                //加载用户信息及水表
                txtYongHuH.Text = YONGHUH;
                LoadData(false);
                //禁用用户区域文本框及查询按钮
                txtYongHuH.Enabled = false;
                btnSearch.Enabled = false;
            }
            else
            {
                txtYongHuH.Enabled = true;
                btnSearch.Enabled = true;
            }
            txtYongHuM.Enabled = false;
            txtDiZhi.Enabled = false;
        }


        /// <summary>
        /// 查询用户下水表
        /// </summary>
        private void LoadData(bool alert)
        {
            if (!string.IsNullOrEmpty(txtYongHuH.Text))
            {
                DataRowCollection drc = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 and S_Yonghuh='{0}' and i_leixing=1", txtYongHuH.Text, BusinessTool.ZHANDIANBH() == "00" ? "" : " and S_ZHANDIANBH='" + BusinessTool.ZHANDIANBH() + "'"), null).Rows;
                if (drc.Count == 1)
                {
                    //记忆当前用户号
                    YONGHUH = (drc[0]["S_YONGHUH"]).ToString();
                    txtYongHuM.Text = drc[0]["S_YONGHUM"].ToString();
                    txtDiZhi.Text = drc[0]["S_DIZHI"].ToString();
                    ZHANDIANBH = drc[0]["S_ZHANDIANBH"].ToString();
                    //加载其下水表
                    string sWhere = "'------'";
                    foreach (DataRow dr in _YS_CB_YONGHUSBGXBLL.SelectYS_CB_YONGHUSBGXesDynamic(string.Format("I_JLZT=0 and I_LEIXING=1 and S_YONGHUBH='{0}' and I_SHUIBIAOZT=0", txtYongHuH.Text), null).Rows)
                    {
                        sWhere += ",'" + dr["S_SHUIBIAOBH"].ToString() + "'";
                    }
                    dtSource = _YS_CB_SHUIBIAOXXBLL.SelectYS_CB_SHUIBIAOXXesDynamic(String.Format("I_LEIXING=1 and S_SHUIBIAOBH in ({0})", sWhere), null);
                    gvList1.DataSource = dtSource;
                    gvList1.DataBind();
                    //SetDeleteVisible();
                }
                else
                {
                    if (alert)
                    {
                        PageTools.AlertMsg(this, "该用户不存在,请重新输入！");
                    }
                }
            }
        }

        /// <summary>
        /// 根据输入的水表号查询水表
        /// </summary>
        //private void LoadData2(bool alert)
        //{
        //    //计算要查询的水表ID
        //    string bhs = "";
        //    foreach (var item in shuiBiaoBHs.Keys)
        //    {
        //        bhs = bhs + item + ",";
        //    }
        //    if (txtConShuiBiaoH.Text != "")
        //    {
        //        //查询该水表对应情况,判断是否可用
        //        DataTable dtTemp = _spHelper.P_YS_CB_PANDUANSHZBKY(txtConShuiBiaoH.Text.Trim(), txtYongHuH.Text, txtYongHuM.Text).Tables[0];
        //        if (Convert.ToString(dtTemp.Rows[0][0]) == "-1")
        //        {
        //            //判断查询的水表号是否已经存在记忆列表中,不存在则添加
        //            if (!shuiBiaoBHs.ContainsKey(txtConShuiBiaoH.Text))
        //            {
        //                shuiBiaoBHs.Add(txtConShuiBiaoH.Text, txtConShuiBiaoH.Text);
        //                bhs = bhs + txtConShuiBiaoH.Text + ",";
        //            }
        //            if (bhs == "")
        //            {
        //                return;
        //            }
        //            bhs = "," + bhs;
        //            //查询
        //            DataTable dt = _YS_CB_ZILAISGSSJBBLL.SelectYS_CB_ZILAISGSSJsDynamic(
        //                string.Format("instr('{0}',','||S_SHUIBIAOBH||',',1,1)>0", bhs),
        //                string.Format("instr('{0}',','||S_SHUIBIAOBH||',',1,1)", bhs));
        //            dtSource2 = dt;
        //            gvList2.DataSource = dtSource2;
        //            gvList2.DataBind();
        //            //定位刚查询的数据
        //            if (dtSource2.Rows.Count > 0)
        //            {
        //                PageTools.FocusOpreateRecord(dtSource2, gvList2, "S_SHUIBIAOBH", txtConShuiBiaoH.Text, true);
        //            }
        //        }
        //        else
        //        {
        //            PageTools.AlertMsg(this,"水表未被考核");
        //        }
                
        //    }
            
        }
    }